/*----------------------------------------------------------------------------*\
| /gamemode/zombies.lua                | zombie manipulation system      | SRV |
\*----------------------------------------------------------------------------*/

function CalculateZombieDamage( ent, hitgroup, dmg )
	// Checks to see if the victim is a zombie and is valid
	if (ent:IsValid() and ent:GetClass() == "npc_zombie") then
		// If the attacker is a NPC, we need to compensate by making the damage
		// slightly higher and make it hurt regardless of where the zombie is
		// hit (because citizens suck at aiming for the head)
		if (dmg:GetAttacker():GetClass() == "npc_citizen") then
			dmg:ScaleDamage(2.5)
			return dmg
		end
		
		// Make human players do far less damage unless the head is hit
		if (hitgroup>=2) then
			dmg:ScaleDamage(0.1)
			return dmg
		end
	end
end
hook.Add( "ScaleNPCDamage", "CalculateZombieDamage", CalculateZombieDamage )

function CustomRagdollZombies( ent, killer, weapon )
	if ( ent:GetClass() == "npc_zombie" ) then
		local model = ZombieModels[1]
		if (util.IsValidModel( ent:GetName() )) then
			model = ent:GetName()
		end
		local rag = ents.Create( "prop_ragdoll" )
		rag:SetKeyValue("origin", ent:GetPos().x.." "..ent:GetPos().y.." "..ent:GetPos().z + 1)
		rag:SetKeyValue("angles", ent:GetAngles().p.." "..ent:GetAngles().y.." "..ent:GetAngles().r)
		rag:SetModel(model)
		rag:SetName("NOREMOVE")
		rag:Spawn()
		rag:SetCollisionGroup( COLLISION_GROUP_DEBRIS )
		rag:Fire("kill",1,10)
		ent:Remove()
		
		if (killer:IsPlayer()) then
			score.zombies.killed[killer:UserID()] = score.zombies.killed[killer:UserID()] + 1
		end
	end
end
hook.Add( "OnNPCKilled", "CustomRagdollZombies", CustomRagdollZombies )

function PreloadNewZombieModels()
	local entity = ""
	for k,v in pairs( ZombieModels ) do
		entity = ents.Create( "npc_zombie" )
		entity:SetPos( Vector( 0, 0, 0 ) )
		entity:Spawn()
		entity:SetModel(v)
		entity:Remove()
	end
end
hook.Add( "InitPostEntity", "PreloadNewZombieModels", PreloadNewZombieModels )

function ForceNewZombieModels()
	for k,v in pairs( table.Merge( ents.FindByClass("npc_zombie"), ents.FindByClass("npc_zombie_torso") ) ) do
		if v != nil then
			model = ZombieModels[1]
			v:SetModel( model )
			// disabled for now, it breaks scripted zombie events
			// TODO: Find a better way to do this? Perhaps a global table? -takua108
			//v:SetName( model )
		end
	end
end
hook.Add( "InitPostEntity", "ForceNewZombieModels", ForceNewZombieModels )
